package com.hwz.gulimall.product.service.impl;

import com.hwz.gulimall.product.entity.SkuImagesEntity;
import com.hwz.gulimall.product.entity.SpuInfoDescEntity;
import com.hwz.gulimall.product.service.*;
import com.hwz.gulimall.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.Query;

import com.hwz.gulimall.product.dao.SkuInfoDao;
import com.hwz.gulimall.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;


    @Autowired
    private ThreadPoolExecutor executor;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(params.get("key"))){
            skuInfoEntityQueryWrapper.and(obj -> obj.eq("sku_id", params.get("key")).or().like("sku_name", params.get("key")));
        }
        if (!StringUtils.isEmpty(params.get("catelogId")) && !"0".equals(params.get("catelogId"))){
            skuInfoEntityQueryWrapper.eq("catalog_id", params.get("catelogId"));
        }
        if (!StringUtils.isEmpty(params.get("brandId")) &&  !"0".equals(params.get("brandId"))){
            skuInfoEntityQueryWrapper.eq("brand_id", params.get("brandId"));
        }
        if (!StringUtils.isEmpty(params.get("min")) && !StringUtils.isEmpty(params.get("max")) &&  !"0".equals(params.get("max"))){
            skuInfoEntityQueryWrapper.between("price", params.get("min"), params.get("max"));
        }



        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                skuInfoEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();


        //异步编排执行
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        }, executor);


        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(skuInfo -> {
            List<SkuItemVo.SkuItemSaleAttrVo> saleAttr = skuSaleAttrValueService.getSaleAttrBySpuId(skuInfo.getSpuId());
            skuItemVo.setSaleAttr(saleAttr);
        }, executor);

        CompletableFuture<Void> spuInfoDescFuture = infoFuture.thenAcceptAsync(skuInfo -> {
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(skuInfo.getSpuId());
            skuItemVo.setDesc(spuInfoDesc);
        }, executor);

        CompletableFuture<Void> spuAttrFuture = infoFuture.thenAcceptAsync(skuInfo -> {
            attrGroupService.getAttrGroupWithAttrsBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupService.getAttrGroupWithAttrsBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId()));
        }, executor);


        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

        CompletableFuture.allOf(saleAttrFuture, spuInfoDescFuture, spuAttrFuture, imageFuture).get();

        //1、查询sku基础信息
//        SkuInfoEntity skuInfo = getById(skuId);
//        skuItemVo.setInfo(skuInfo);
        //2、查询sku图片信息
//        List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
//        skuItemVo.setImages(images);
//        //3、查询spu销售属性
//        List<SkuItemVo.SkuItemSaleAttrVo> saleAttr = skuSaleAttrValueService.getSaleAttrBySpuId(skuInfo.getSpuId());
//        skuItemVo.setSaleAttr(saleAttr);

        //4、查询spu的描述信息
//        SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(skuInfo.getSpuId());
//        skuItemVo.setDesc(spuInfoDesc);
        //5、查收spu的规格参数信息
//        attrGroupService.getAttrGroupWithAttrsBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId());
//        skuItemVo.setGroupAttrs(attrGroupService.getAttrGroupWithAttrsBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId()));

        return skuItemVo;
    }

}